/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Prospect;
import MODEL.Reserver;
import MODEL.ReserverPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class ReserverJpaController implements Serializable {

    public ReserverJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reserver reserver) throws PreexistingEntityException, Exception {
        if (reserver.getReserverPK() == null) {
            reserver.setReserverPK(new ReserverPK());
        }
        reserver.getReserverPK().setIdPro(reserver.getProspect().getIdPro());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Prospect prospect = reserver.getProspect();
            if (prospect != null) {
                prospect = em.getReference(prospect.getClass(), prospect.getIdPro());
                reserver.setProspect(prospect);
            }
            em.persist(reserver);
            if (prospect != null) {
                prospect.getReserverCollection().add(reserver);
                prospect = em.merge(prospect);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReserver(reserver.getReserverPK()) != null) {
                throw new PreexistingEntityException("Reserver " + reserver + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reserver reserver) throws NonexistentEntityException, Exception {
        reserver.getReserverPK().setIdPro(reserver.getProspect().getIdPro());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserver persistentReserver = em.find(Reserver.class, reserver.getReserverPK());
            Prospect prospectOld = persistentReserver.getProspect();
            Prospect prospectNew = reserver.getProspect();
            if (prospectNew != null) {
                prospectNew = em.getReference(prospectNew.getClass(), prospectNew.getIdPro());
                reserver.setProspect(prospectNew);
            }
            reserver = em.merge(reserver);
            if (prospectOld != null && !prospectOld.equals(prospectNew)) {
                prospectOld.getReserverCollection().remove(reserver);
                prospectOld = em.merge(prospectOld);
            }
            if (prospectNew != null && !prospectNew.equals(prospectOld)) {
                prospectNew.getReserverCollection().add(reserver);
                prospectNew = em.merge(prospectNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ReserverPK id = reserver.getReserverPK();
                if (findReserver(id) == null) {
                    throw new NonexistentEntityException("The reserver with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ReserverPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserver reserver;
            try {
                reserver = em.getReference(Reserver.class, id);
                reserver.getReserverPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reserver with id " + id + " no longer exists.", enfe);
            }
            Prospect prospect = reserver.getProspect();
            if (prospect != null) {
                prospect.getReserverCollection().remove(reserver);
                prospect = em.merge(prospect);
            }
            em.remove(reserver);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reserver> findReserverEntities() {
        return findReserverEntities(true, -1, -1);
    }

    public List<Reserver> findReserverEntities(int maxResults, int firstResult) {
        return findReserverEntities(false, maxResults, firstResult);
    }

    private List<Reserver> findReserverEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Reserver.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reserver findReserver(ReserverPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reserver.class, id);
        } finally {
            em.close();
        }
    }

    public int getReserverCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Reserver> rt = cq.from(Reserver.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
